//
//  AppTVDetailController.m
//  classicFilms
//
//  Created by 刘继丹 on 17/3/14.
//  Copyright © 2017年 Stefan Arn. All rights reserved.
//

#import "AppTVDetailController.h"
#import "AppTVDetailView.h"
#import "AppTVTopLoopView.h"
#import "KeyHeader.h"
#import "AppTVVideoPlayController.h"
#import <AVKit/AVKit.h>
#import "UIImageView+WebCache.h"
#import "AppTVBigImageBrowseController.h"
#import "ServiceDetailApi.h"
#import "ServiceDetailBottomApi.h"
#import "YTKBaseRequest+AnimatingAccessory.h"
#import "ServiceDetailBottomApi.h"
#import "ARNTextViewController.h"

#define kBottomViewHeight 330

@interface AppTVDetailController ()

@property(nonatomic, strong) AVPlayer *player;

@property(nonatomic, strong) NSMutableArray *mImageArr;

@property(nonatomic, strong) AppTVDetailView *mAppTVDetailView;

@property(nonatomic, strong) UIView *mBottomView;
@property(nonatomic, strong) UILabel *mTitleLabel;
@property(nonatomic, strong) AppTVTopLoopView *mAppTVTopLoopView;

@property(nonatomic, strong) ServiceDetailModel *mServiceDetailModel;
@property(nonatomic, strong) NSArray *mDataArr;


@end

@implementation AppTVDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    self.view.backgroundColor = [UIColor blackColor];
    
    //底部视图
    [self.view addSubview:self.mBottomView];
    [self.mBottomView addSubview:self.mTitleLabel];
    [self.mBottomView addSubview:self.mAppTVTopLoopView];
    
    [self loadNewData];
    
    [self loadBottomData];
    
}

-(void)loadNewData{
   
    ServiceDetailApi *detailApi = [[ServiceDetailApi alloc]initWithList_id:_list_id];
    detailApi.animatingText = nil;
    detailApi.animatingView = self.view;
    
    [detailApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        
//        NSLog(@"=====%@", request.responseJSONObject);
        
        _mServiceDetailModel = request.responseModelObject;
        [self.view addSubview:self.mAppTVDetailView];
        _mAppTVDetailView.dataModel = _mServiceDetailModel.data;

    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        
    }];
}


-(void)loadBottomData{

    ServiceDetailBottomApi *detailBottomApi = [[ServiceDetailBottomApi alloc]initWithCraftsmans_id:_list_id];
    
    [detailBottomApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        
//        NSLog(@"🍏====%@", request.responseJSONObject);
        
        NSArray *array = [[request.responseJSONObject objectForKey:@"data"] objectForKey:@"lists"];
        _mDataArr = array;
        
        if (_type==0) {
            _mAppTVTopLoopView.isHidden_playBtn=NO;
        }else{
            _mAppTVTopLoopView.isHidden_playBtn=YES;
        }
        
        _mAppTVTopLoopView.imageArr = _mDataArr;
        
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        
    }];
    
}

#pragma mark- configView
-(AppTVDetailView *)mAppTVDetailView{
    if (!_mAppTVDetailView) {
        _mAppTVDetailView = [[AppTVDetailView alloc]initWithFrame:CGRectMake(0, 0, KVIEW_W, KVIEW_H-kBottomViewHeight)];
        
        _mAppTVDetailView.dataModel = nil;
        
        __weak typeof(self) weakSelf = self;
        
        _mAppTVDetailView.detailLabelTouchBlock = ^(NSString *content){
            ARNTextViewController *textViewController = [ARNTextViewController new];
            textViewController.label.text = content;
            textViewController.modalPresentationStyle = UIModalPresentationOverFullScreen;
            [weakSelf presentViewController:textViewController animated:YES completion:nil];
        };

    }
    return _mAppTVDetailView;
}


-(UIView *)mBottomView{
    if (!_mBottomView) {
        _mBottomView = [[UIView alloc]initWithFrame:CGRectMake(0, KVIEW_H-kBottomViewHeight, KVIEW_W, kBottomViewHeight)];
        _mBottomView.backgroundColor = kDetailBottomBackGroundColor_black;
        [_mBottomView addLineUp:YES andDown:NO];
    }
    return _mBottomView;
}

-(UILabel *)mTitleLabel{
    if (!_mTitleLabel) {
        _mTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 0, KVIEW_W-90, 70)];
        _mTitleLabel.text = @"全部作品";
        _mTitleLabel.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7];
        _mTitleLabel.font = [UIFont systemFontOfSize:20];
    }
    return _mTitleLabel;
}

-(AppTVTopLoopView *)mAppTVTopLoopView{
    if (!_mAppTVTopLoopView) {
        _mAppTVTopLoopView = [[AppTVTopLoopView alloc]initWithFrame:CGRectMake(0, 60, KVIEW_W, kBottomViewHeight-120) type:AppTVLoopViewType_detail];

        __weak typeof(self) weakSelf = self;
        //图片选择
        _mAppTVTopLoopView.imageSelectBlock = ^(NSInteger selectIndex){
            
            if (_type == 0)  {
                
                NSDictionary *dic = [weakSelf.mDataArr objectAtIndex:selectIndex];
                NSString *video = [dic valueForKey:@"src"];
                [weakSelf loadMovie:video];
                [weakSelf playMovie];

                
            }else
            {
                NSDictionary *dic = [weakSelf.mDataArr objectAtIndex:selectIndex];
                NSArray *imageArr = [dic valueForKey:@"img_src"];

                AppTVBigImageBrowseController *imageBrowse =[[AppTVBigImageBrowseController alloc]init];
                
                NSMutableArray *dataArr = [[NSMutableArray alloc] init];
                for (int i = 0; i<imageArr.count; i++) {
                    NSDictionary *dic = [imageArr objectAtIndex:i];
                    NSString *name = [dic valueForKey:@"url"];
                    [dataArr addObject:name];
                }
                
                imageBrowse.imageArr = dataArr;
                [weakSelf presentViewController:imageBrowse animated:YES completion:^{}];
            }
            

            
        };
    }
    return _mAppTVTopLoopView;
}


- (void)loadMovie:(NSString*)video {
    
    NSURL *videoURL = [NSURL URLWithString:video];
    
    // start the player
    self.player = [AVPlayer playerWithURL:videoURL];
    
    // subscribe to be informed about a finished playback
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidPlayToEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.player.currentItem];
    
}

- (void)playMovie {
    if (self.player != nil ) {
        AVPlayerViewController *playerViewController = [AVPlayerViewController new];
        [self presentViewController:playerViewController animated:YES completion:^{
            playerViewController.player = self.player;
            [self.player play];
        }];
    }
}

- (void)itemDidPlayToEnd:(NSNotification *)notification {
    // will be called when AVPlayer finishes playing
    if(self.presentedViewController != nil && [self.presentedViewController isKindOfClass:[AVPlayerViewController class]]) {
        [self.presentedViewController dismissViewControllerAnimated:YES completion:^{
            // reset the player to the start of the video.
            // we do this to prevent the following behaviour:
            // if an user starts the movie a second time we would
            // still be at the end and dismiss ourselves immediately.
            // the user would not be able to start the video again.
            if (self.player != nil) {
                [self.player seekToTime:CMTimeMakeWithSeconds(0.0f, 1)];
            }
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
